<?php
/**
 * it makes an autoFiltered list from the selected row of the $stream array
 * 
 *it makes an autoFiltered list from the selected row of the $stream array, like this 
 * <code>
 *      .<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/C/">C</a>. 
 *  	.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/E/">E</a>. 		
 *		.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/I/">I</a>.
 *		.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/P/">P</a>. 
 *		.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/R/">R</a>. 		
 *		.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/T/">T</a>. 
 *		.<a href="&document.url.value;data/books/&argv_nav.rowselect.value;/filter/W/">W</a>. 
 *		.<a href="&document.url.value;">All</a>.
 * </code>
 *
 * @param array $stream
 * @param string $filtered_row
 * @param string $filtered_data_path
 */
function get_filter_list($addparams=false){
    global $sapi_obj, $env, $http_path, $lang,$data,$root_path;
//    filter_list only works in 'entry in list' mode
    if ($env["virtual_path_mask"]) return $stream=array();

    // If the DDC parameter QUERY is passed over this CMS application then replace $addparams value with it
    if($sapi_obj->env["this.param.query"]) {
        $addparams = $sapi_obj->env["this.param.query"];
        $sapi_obj->env["this.param.query"] = false;
    }

    // Analize input parameters

    if( preg_match("/_CHANNEL\(/s", $addparams) )  {
        preg_match("/(?<=_CHANNEL\().+?(?=\)_)/s",$addparams, $CHANNEL);
        $CHANNEL=$CHANNEL[0];
    }
    if( preg_match("/_NAVURL\(/s", $addparams) ) $NAVURL =  preg_replace("/^.*?_NAVURL\((.*?)\)_.*?$/s", "\\1", $addparams); 
    if( preg_match("/_BASEURL\(/s", $addparams) ) $BASEURL =  preg_replace("/^.*?_BASEURL\((.*?)\)_.*?$/s", "\\1", $addparams);
    if( preg_match("/_FILTERROW\(/s", $addparams) ) $FILTERROW =  preg_replace("/^.*?_FILTERROW\((.*?)\)_.*?$/s", "\\1", $addparams);

    if(!$CHANNEL) $CHANNEL = "books";
    $filename=$GLOBALS["root_path"] . "usr/xml/vdb/".$CHANNEL.".xml";
    $channelpath=$GLOBALS["root_path"] . "usr/xml/vdb/".$CHANNEL;
    
    $NAVURL=($NAVURL)?$NAVURL:$BASEURL;

    $sapi_obj->env["this.channel"]= $CHANNEL;

    // Get rows of the channel
    $db = new sapi_vdb($filename);
    $stream=$db->get_data();
    $db->close();


    // Get content

    foreach($stream as $inx => $f) {
        if((!$limit_start OR $counter>=$limit_start) AND (!$limit_finish OR $counter<=$limit_finish)) {
            $fetch = $db->get_data($channelpath."/".($f["ID"]).".xml", "_ONEROW_");

            if($f["DATE_CREATE"]) $fetch["DATE_CREATE"] = $f["DATE_CREATE"];

            // Upload control
            if($fetch) {
                foreach($fetch as $inx1 => $val1) {
                    if($upload_data[$inx1]) $fetch[$inx1] = $upload_data[$inx1];
                }
            }

            if($fetch) $stream[$inx] = $fetch; else unset($stream[$inx]);
        } else unset($stream[$inx]);


    }

    $already_in_array=array();$outArray=array();
    $selected_row=strtoupper((!$FILTERROW)?'BOOK_TITLE':$FILTERROW);

    foreach ($stream as $key=>$list_array){
        $filtered_list_item=strtoupper((substr($list_array[$selected_row],0,1)));
        if (!in_array($filtered_list_item,$already_in_array)){
            $already_in_array[]=$filtered_list_item;
            $outArray[$filtered_list_item]['filter']= '.<a href="&document.url.value;'.$NAVURL.'&argv_nav.rowselect.value;/filter'.$CHANNEL.'/'.$filtered_list_item.'/">'.$filtered_list_item.'</a>';
            
           
        }
    }
    
    usort ($outArray, "alphaSort");
    $stream=$outArray;
    $stream[sizeof($stream)]['filter']='.<a href="&document.url.value;">All</a>.';
    
    return $stream;
}


function alphaSort($a, $b){
    return ($a["filter"] < $b["filter"])? -1:1;
}


?>